const app = getApp();
const server = require("../../server/server.js");
Page({
    data:{
        flag:false,
        user:{}
    },
    getUser({detail}) {
        // console.log(detail);
        // console.log(app.userInfo);
        let user = app.userInfo;
        if(user){
            server.addUser({
                headurl:user.avatarUrl,
                name:user.nickName,
                openid:user.openId
            }).then((data)=>{
                // console.log(data);
                user.id = data.result;
                this.setData({
                    flag:true,
                    user:user
                });
                app.userInfo = user;
            })
        }else{
            user = detail.userInfo;
            wx.cloud.callFunction({
                name:"getUserOpenId",
                success:(data)=>{
                    user.openId = data.result.openid;

                    server.addUser({
                        headurl:user.avatarUrl,
                        name:user.nickName,
                        openid:user.openId
                    }).then((data)=>{
                        console.log(data);
                        user.id = data.result;
                        this.setData({
                            flag:true,
                            user:user
                        });
                        app.userInfo = user;
                    })
                }
            }) 
        }
    },
    onpageshow(){
        if(app.userInfo){
            if(app.userInfo.id){
                this.serData({
                    flag:true,
                    user:app.userInfo
                });
            }else{
                this.serData({
                    flag:false
                })
            }
        }else{
            this.serData({
                flag:false
            })
        }
    }
})